package com.tonyodev.fetch2fileserver.database;


import ohos.app.Context;

import java.util.List;

public class FetchFileResourceInfoDatabase implements Cloneable {
    private Context context;
    private String databaseName;
    private Object lock = new Object();
    private boolean closed = false;
    private FileResourceInfoDatabase fileResourceInfoDatabase;
    private FileResourceInfoDao fileResourceInfoDao;

    public FetchFileResourceInfoDatabase(Context context, String databaseName) {
        this.context = context;
        this.databaseName = databaseName;
        this.fileResourceInfoDatabase = new FileResourceInfoDatabase(context, databaseName);
        this.fileResourceInfoDao = fileResourceInfoDatabase.getInstance();
    }

    public long insert(FileResourceInfo fileResourceInfo) {
        synchronized (lock) {
            throwExceptionIfClosed();
            return fileResourceInfoDao.insert(fileResourceInfo);
        }
    }

    public List<Long> insert(List<FileResourceInfo> fileResourceInfoList) {
        synchronized (lock) {
            throwExceptionIfClosed();
            return fileResourceInfoDao.insert(fileResourceInfoList);
        }
    }

    public void delete(FileResourceInfo fileResourceInfo) {
        synchronized (lock) {
            throwExceptionIfClosed();
            fileResourceInfoDao.delete(fileResourceInfo);
        }
    }

    public void delete(List<FileResourceInfo> fileResourceInfoList) {
        synchronized (lock) {
            throwExceptionIfClosed();
            fileResourceInfoDao.delete(fileResourceInfoList);
        }
    }

    public void deleteAll() {
        synchronized (lock) {
            throwExceptionIfClosed();
            fileResourceInfoDao.deleteAll();
        }
    }

    public List<FileResourceInfo> get() {
        synchronized (lock) {
            throwExceptionIfClosed();
            return fileResourceInfoDao.get();
        }
    }

    public FileResourceInfo get(long id) {
        synchronized (lock) {
            throwExceptionIfClosed();
            return fileResourceInfoDao.get(id);
        }
    }

    public List<FileResourceInfo> get(List<Long> ids) {
        synchronized (lock) {
            throwExceptionIfClosed();
            return fileResourceInfoDao.get(ids);
        }
    }

    public FileResourceInfo get(String fileName) {
        synchronized (lock) {
            throwExceptionIfClosed();
            return fileResourceInfoDao.get(fileName);
        }
    }

    //目前分页有问题
    public List<FileResourceInfo> getAll(int page, int size) {
        synchronized (lock) {
            throwExceptionIfClosed();
            if (page == -1 && size == -1) {
                return fileResourceInfoDao.get();
            } else {
                int offset = 0;
                for (int i = 0; i < page; i++) {
                    offset = size * page;
                }
                return fileResourceInfoDao.getPage(page, offset);
            }
        }
    }

    public String getRequestedCatalog() {
        return getRequestedCatalog(-1, -1);
    }

    //特别注意
    public String getRequestedCatalog(int page, int size) {
        synchronized (lock) {
            throwExceptionIfClosed();
            List<FileResourceInfo> fileResourceInfos = getAll(page, size);
            StringBuilder stringBuilder = new StringBuilder("{\"catalog\":[");
            for (int i = 0; i < fileResourceInfos.size(); i++) {
                FileResourceInfo fileResource = fileResourceInfos.get(i);
                stringBuilder.append("{\"id\":" + fileResource.getId()
                    + ",\"name\":\"" + fileResource.getName() + "\","
                    +"\"length\":" + fileResource.getLength()
                    + ",\"extras\":" + fileResource.getExtras()
                    + "," +
                    "\"md5\":\"" + fileResource.getMd5() + "\"}");
                if (i != fileResourceInfos.size() - 1) {
                    stringBuilder.append(",");
                }
            }
            StringBuilder builder = stringBuilder.append("],\"size\":" + fileResourceInfos.size() + "}");
            return builder.toString();
        }
    }

    public void close() {
        synchronized (lock) {
            if (closed) {
                return;
            }
            closed = true;
            fileResourceInfoDatabase.close();
        }
    }

    public boolean isClosed() {
        return closed;
    }

    public void setClosed(boolean closed) {
        this.closed = closed;
    }

    private void throwExceptionIfClosed() {
        try {
            if (closed) {
                throw new Exception("FetchFileResourceInfoServerDatabase is closed");
            }
        } catch (Exception e) {
            System.out.println("错误信息" + e.getMessage());
        }

    }
}
